var JSObject = {

    selected_row_id:null,
    objGrid:null,

    init:function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url:$('#table-url').val(),
            reload_after_add:true,
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:false, align:'left', hide:true},
                {display:'Customer Name', name:'customer_id', width:220, sortable:true, align:'left', hide:false, editable:{form:'select', validate:'', defval:'',options:customers}},
                {display:'Next Service Date', name:'next_service_date', width:150, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'empty', defval:'',placeholder:'yyyy-mm-dd'}},
                {display:'Reminder Time', name:'reminder_time', width:150, sortable:true, align:'left', hide:false, editable:{form:'select', validate:'', defval:'',options:reminder_times}},
                {display:'Reminder Status', name:'reminded', width:150, sortable:true, align:'left', hide:false},
                {display:'Service Status', name:'serviced', width:150, sortable:true, align:'left', hide:false}
            ],
            formFields:[
                {type:'buttom', name:'New', bclass:'add', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Save', bclass:'save', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Edit', bclass:'edit', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Cancel', bclass:'cancel', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Delete', bclass:'delete', onpress:self.handleGridEvent},
                {separator:true},
                {type:'select',name: 'Filter Customer', id: 'filter_customer',bclass: 'filter',onchange:self.handleGridEvent,options:customers_filter},
                {separator:true},
                {type:'select',name: 'Filter Reminded', id: 'filter_reminded',bclass: 'filter',onchange:self.handleGridEvent,options:reminded_filter},
                {separator:true},
                {type:'select',name: 'Filter Serviced', id: 'filter_serviced',bclass: 'filter',onchange:self.handleGridEvent,options:serviced_filter}
                /*{type:'buttom', name:'Export All', bclass:'export', onpress:self.handleGridEvent}*/
                //,
                //{separator:true}
            ],
            /*searchitems:[
                {display:'Customer Name', name:'name', isdefault:true},
                {display:'Telephone', name:'telephone'},
                {display:'Email', name:'email'},
                {display:'Address', name:'address'}
             ],*/
            checkboxSelection:true,
            editablegrid:{
                use:true,
                url:$('#table-editable-url').val()
            },
            columnControl:false,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            subgrid:{
                use:false
            }
        });
    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'New') {
            JSObject.objGrid.flexBeginAdd();
        }
        else if (com == 'Edit') {
            var r = FlexObject.rowSelectedCheck(JSObject.objGrid,grid,1);
            if(r){
                var row = FlexObject.getSelectedRows(grid);
                JSObject.objGrid.flexBeginEdit(row[0]);
            }
        }
        else if (com == 'Save') {
            JSObject.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            JSObject.objGrid.flexCancel();
        }
        else if (com == 'Delete') {
            var r = FlexObject.rowSelectedCheck(JSObject.objGrid,grid);
            if(r){
                JSObject.delete_(grid);
            }
        }
        else if (com == 'Export All') {
            var url = $("#export_url").val();
            window.open(url, "PrintExportWindow", "menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
        }
        else if (com == 'Filter Customer' || com == 'Filter Reminded' || com == 'Filter Serviced') {
            JSObject.filterGrid(json);
        }
    },

    filterGrid:function(json){
        var self = this;
        var filter_customer = $("#filter_customer").val();
        var filter_reminded = $("#filter_reminded").val();
        var filter_serviced = $("#filter_serviced").val();
        $(self.objGrid).flexOptions({
            params: [
                {name: 'filter_customer', value: filter_customer},
                {name: 'filter_reminded', value: filter_reminded},
                {name: 'filter_serviced', value: filter_serviced}
            ]
        }).flexReload();
    },

    delete_:function (grid) {
        var self = this;
        var url = $('#delete_url').val();
        jLib.do_delete(url, grid,function(response){
            if(response.code == 0){
                FlexObject.removeGridRows(grid);
                self.objGrid.flexNotify('Status', response.msg, 'success');
            }
            else if(response.code == 1){
                self.objGrid.flexNotify('Status', response.msg, 'error');
            }
        });
    }
};

/* when the page is loaded */
$(document).ready(function () {
    JSObject.init();
});